package IO流.IO流操作.节点流.inputStream和outputStream.文件和文件夹拷贝;

import java.io.*;

public class DirectoryCopy {
    public static void copyDirectoryOrFile(String path, String copyPath) {
        File file = new File(path);
        File copyFile = new File(copyPath);
        InputStream inputStream = null;
        OutputStream outputStream = null;

        try {
            if (!file.exists()) {
                    return;
            } else {
                //是文件则

                if (file.isFile()) {
                    inputStream = new FileInputStream(file);
                    outputStream = new FileOutputStream(copyFile);
                    byte[] buffer = new byte[1024];
                    while (inputStream.read(buffer) != -1) {
                        outputStream.write(buffer);
                        outputStream.flush();
                    }
                    return;
                } else {//进入其子孙级，递归其大小
                    copyFile.mkdirs();
                    for (File f : file.listFiles()) {
                        String copyCopyPath = copyPath + File.separator + f.getName();
                        copyDirectoryOrFile(f.getAbsolutePath(), copyCopyPath);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != inputStream) {
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (null != outputStream) {
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    public static void main(String[] args) {
        DirectoryCopy.copyDirectoryOrFile("D:\\layui作业黄志", "D:\\layui作业");
    }
}
